<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="客户编号" prop="number">
        <el-input v-model="formData.number" placeholder="请输入客户编号" />
      </el-form-item>
      <el-form-item label="客户名称" prop="name">
        <el-input v-model="formData.name" placeholder="请输入客户名称" />
      </el-form-item>
      <el-form-item label="客户全称" prop="customerFullName">
        <el-input v-model="formData.customerFullName" placeholder="请输入客户全称" />
      </el-form-item>
      <el-form-item label="客户类型" prop="customerType">
        <el-select v-model="formData.customerType" placeholder="请选择客户类型">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.BASE_CUSTOMER_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="集团内外" prop="insideGroup">
        <el-select v-model="formData.insideGroup" placeholder="请选择集团内外">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.BASE_INSIDE_GROUP)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="报关联系" prop="declareContacts">
        <el-input v-model="formData.declareContacts" placeholder="请输入报关联系" />
      </el-form-item>
      <el-form-item label="送货联系" prop="deliveryContacts">
        <el-input v-model="formData.deliveryContacts" placeholder="请输入送货联系" />
      </el-form-item>
      <el-form-item label="财务联系" prop="financialContacts">
        <el-input v-model="formData.financialContacts" placeholder="请输入财务联系" />
      </el-form-item>
      <el-form-item label="本厂业务员" prop="factoryBusinessPerson">
        <el-input v-model="formData.factoryBusinessPerson" placeholder="请输入本厂业务员" />
      </el-form-item>
      <el-form-item label="本厂销售员" prop="factorySalesPerson">
        <el-input v-model="formData.factorySalesPerson" placeholder="请输入本厂销售员" />
      </el-form-item>
      <el-form-item label="电话" prop="customerPhone">
        <el-input v-model="formData.customerPhone" placeholder="请输入电话" />
      </el-form-item>
      <el-form-item label="传真" prop="customerFax">
        <el-input v-model="formData.customerFax" placeholder="请输入传真" />
      </el-form-item>
      <el-form-item label="负责人" prop="leader">
        <el-input v-model="formData.leader" placeholder="请输入负责人" />
      </el-form-item>
      <el-form-item label="回扣率" prop="kickbackRate">
        <el-input v-model="formData.kickbackRate" placeholder="请输入回扣率" />
      </el-form-item>
      <el-form-item label="业务比" prop="businessRate">
        <el-input v-model="formData.businessRate" placeholder="请输入业务比" />
      </el-form-item>
      <el-form-item label="公司地址" prop="customerAddress">
        <el-input v-model="formData.customerAddress" placeholder="请输入公司地址" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" placeholder="请输入备注" />
      </el-form-item>
      <el-form-item label="单价处理方式" prop="unitPriceRoundingMode">
        <el-select v-model="formData.unitPriceRoundingMode" placeholder="请选择单价处理方式">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.ERP_DECIMAL_ROUNDING_MODE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="单价小数位" prop="unitPriceDecimalPlaces">
        <el-select v-model="formData.unitPriceDecimalPlaces" placeholder="请选择单价小数位">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.DECIMAL_ROUND_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="金额处理方式" prop="totalPriceRoundingMode">
        <el-select v-model="formData.totalPriceRoundingMode" placeholder="请选择金额处理方式">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.ERP_DECIMAL_ROUNDING_MODE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="金额小数位" prop="totalPriceDecimalPlaces">
        <el-select v-model="formData.totalPriceDecimalPlaces" placeholder="请选择金额小数位">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.DECIMAL_ROUND_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="月结天数" prop="monthSettlementPeriod">
        <el-select v-model="formData.monthSettlementPeriod" placeholder="请选择月结天数">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.MONTH_SETTLEMENT_PERIOD)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="每月对帐日" prop="monthSettlementDate">
        <el-input v-model="formData.monthSettlementDate" placeholder="请输入每月对帐日" />
      </el-form-item>
      <el-form-item label="月结折扣" prop="monthSettlementDiscount">
        <el-input v-model="formData.monthSettlementDiscount" placeholder="请输入月结折扣" />
      </el-form-item>
      <el-form-item label="付款方式" prop="paymentMode">
        <el-input v-model="formData.paymentMode" placeholder="请输入付款方式" />
      </el-form-item>
      <el-form-item label="币别" prop="currency">
        <el-select v-model="formData.currency" placeholder="请选择币别">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.CURRENCY)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="默认单位" prop="defaultUnit">
        <el-select v-model="formData.defaultUnit" placeholder="请选择默认单位">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.DEFAULT_UNIT)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="小数处理方式" prop="decimalProcessingMode">
        <el-select v-model="formData.decimalProcessingMode" placeholder="请选择小数处理方式">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.DECIMAL_PROCESSING_MODE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="送货单账套" prop="deliveryNoteAccountSet">
        <el-input v-model="formData.deliveryNoteAccountSet" placeholder="请输入送货单账套" />
      </el-form-item>
      <el-form-item label="送货区域" prop="deliveryArea">
        <el-select v-model="formData.deliveryArea" placeholder="请选择送货区域">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.DELIVERY_AREA)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="送货单字母" prop="deliveryNoteLetter">
        <el-input v-model="formData.deliveryNoteLetter" placeholder="请输入送货单字母" />
      </el-form-item>
      <el-form-item label="送货单显示套件子件" prop="deliveryNoteDisplayItems">
        <el-radio-group v-model="formData.deliveryNoteDisplayItems">
          <el-radio
            v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="报价税率" prop="quoteTaxRate">
        <el-input v-model="formData.quoteTaxRate" placeholder="请输入报价税率" />
      </el-form-item>
      <el-form-item label="订单销售控制" prop="orderSalesControl">
        <el-select v-model="formData.orderSalesControl" placeholder="请选择订单销售控制">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.ORDER_SALES_CONTROL)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="订单允许送给别的客户" prop="orderGiveOthers">
        <el-radio-group v-model="formData.orderGiveOthers">
          <el-radio
            v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="默认结算方式" prop="defaultSettlementMode">
        <el-select v-model="formData.defaultSettlementMode" placeholder="请选择默认结算方式">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.DEFAULT_SETTLEMENT_MODE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="利润率" prop="profitRate">
        <el-input v-model="formData.profitRate" placeholder="请输入利润率" />
      </el-form-item>
      <el-form-item label="算价方式" prop="valuationMode">
        <el-select v-model="formData.valuationMode" placeholder="请选择算价方式">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.PAPER_CARD_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="金额计算公式" prop="valuationFormula">
        <el-select v-model="formData.valuationFormula" placeholder="请选择金额计算公式">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.VALUATION_FORMULA)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="计价单位" prop="valuationUnit">
        <el-select v-model="formData.valuationUnit" placeholder="请选择计价单位">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.VALUATION_UNIT)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="送货里程" prop="deliveryDistance">
        <el-input v-model="formData.deliveryDistance" placeholder="请输入送货里程" />
      </el-form-item>
      <el-form-item label="送货里程单位" prop="deliveryDistanceUnit">
        <el-input v-model="formData.deliveryDistanceUnit" placeholder="请输入送货里程单位" />
      </el-form-item>
      <el-form-item label="送货单行数" prop="deliveryNoteRows">
        <el-select v-model="formData.deliveryNoteRows" placeholder="请选择送货单行数">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.DELIVERY_NOTE_ROWS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="客户类别" prop="customerCategory">
        <el-input v-model="formData.customerCategory" placeholder="请输入客户类别" />
      </el-form-item>
      <el-form-item label="配件生产是否入库" prop="itemsStocked">
        <el-radio-group v-model="formData.itemsStocked">
          <el-radio
            v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="标签格式" prop="labelFormat">
        <el-input v-model="formData.labelFormat" placeholder="请输入标签格式" />
      </el-form-item>
      <el-form-item label="送货单超订单部分转为备品" prop="excessQuantityAsSpare">
        <el-radio-group v-model="formData.excessQuantityAsSpare">
          <el-radio
            v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="通用报价单号" prop="commonQuoteNumber">
        <el-input v-model="formData.commonQuoteNumber" placeholder="请输入通用报价单号" />
      </el-form-item>
      <el-form-item label="净板计价规格方案" prop="processedCardboardValuationScheme">
        <el-input v-model="formData.processedCardboardValuationScheme" placeholder="请输入净板计价规格方案" />
      </el-form-item>
      <el-form-item label="自动创建纸板生产单默认客户" prop="defaultCustomerForCardboardProduction">
        <el-radio-group v-model="formData.defaultCustomerForCardboardProduction">
          <el-radio
            v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="送货单单价来自" prop="deliveryUnitPriceFrom">
        <el-select v-model="formData.deliveryUnitPriceFrom" placeholder="请选择送货单单价来自">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.SEND_PRICE_FROM)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="采用加价设置报价单号" prop="markupPricingQuoteNumber">
        <el-input v-model="formData.markupPricingQuoteNumber" placeholder="请输入采用加价设置报价单号" />
      </el-form-item>
      <el-form-item label="纸板生产单对应的产品类型" prop="cardboardProductionProductType">
        <el-select v-model="formData.cardboardProductionProductType" placeholder="请选择纸板生产单对应的产品类型">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.CARDBOARD_PRODUCTION_PRODUCT_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="客户报价单" prop="customerQuoteNumber">
        <el-input v-model="formData.customerQuoteNumber" placeholder="请输入客户报价单" />
      </el-form-item>
      <el-form-item label="税别" prop="taxType">
        <el-select v-model="formData.taxType" placeholder="请选择税别">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.TAX_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="纸板订单报价方式" prop="cardboardProductionQuoteMode">
        <el-input v-model="formData.cardboardProductionQuoteMode" placeholder="请输入纸板订单报价方式" />
      </el-form-item>
      <el-form-item label="纸板生产单加工费计算公式" prop="cardboardProductionProcessingFeeFormula">
        <el-input v-model="formData.cardboardProductionProcessingFeeFormula" placeholder="请输入纸板生产单加工费计算公式" />
      </el-form-item>
      <el-form-item label="纸板材质面积单价公式" prop="cardboardMaterialAreaUnitPriceFormula">
        <el-input v-model="formData.cardboardMaterialAreaUnitPriceFormula" placeholder="请输入纸板材质面积单价公式" />
      </el-form-item>
      <el-form-item label="纸板加工费方案" prop="cardboardProcessingFeeScheme">
        <el-input v-model="formData.cardboardProcessingFeeScheme" placeholder="请输入纸板加工费方案" />
      </el-form-item>
      <el-form-item label="纸板生产单折扣" prop="cardboardProductionDiscount">
        <el-input v-model="formData.cardboardProductionDiscount" placeholder="请输入纸板生产单折扣" />
      </el-form-item>
      <el-form-item label="纸板加价设置单号" prop="cardboardMarkupPricingSetNumber">
        <el-input v-model="formData.cardboardMarkupPricingSetNumber" placeholder="请输入纸板加价设置单号" />
      </el-form-item>
      <el-form-item label="会计凭证代码" prop="accountingVoucherCode">
        <el-select v-model="formData.accountingVoucherCode" placeholder="请选择会计凭证代码">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.ACCOUNTING_VOUCHER_CODE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="导入模板" prop="importTemplate">
        <el-input v-model="formData.importTemplate" placeholder="请输入导入模板" />
      </el-form-item>
      <el-form-item label="导入拼单模板" prop="groupOrderImportTemplate">
        <el-input v-model="formData.groupOrderImportTemplate" placeholder="请输入导入拼单模板" />
      </el-form-item>
      <el-form-item label="销售订单导入模板" prop="saleOrderImportTemplate">
        <el-input v-model="formData.saleOrderImportTemplate" placeholder="请输入销售订单导入模板" />
      </el-form-item>
      <el-form-item label="导出方案" prop="exportScheme">
        <el-input v-model="formData.exportScheme" placeholder="请输入导出方案" />
      </el-form-item>
      <el-form-item label="拼单导出方案" prop="groupOrderExportScheme">
        <el-input v-model="formData.groupOrderExportScheme" placeholder="请输入拼单导出方案" />
      </el-form-item>
      <el-form-item label="税号" prop="taxNumber">
        <el-input v-model="formData.taxNumber" placeholder="请输入税号" />
      </el-form-item>
      <el-form-item label="账号" prop="bankAccountNumber">
        <el-input v-model="formData.bankAccountNumber" placeholder="请输入账号" />
      </el-form-item>
      <el-form-item label="开户行" prop="bankOfDeposit">
        <el-input v-model="formData.bankOfDeposit" placeholder="请输入开户行" />
      </el-form-item>
      <el-form-item label="开户行地址" prop="bankOfDepositAddress">
        <el-input v-model="formData.bankOfDepositAddress" placeholder="请输入开户行地址" />
      </el-form-item>
      <el-form-item label="总面积位数" prop="totalAreaDecimalPlaces">
        <el-input v-model="formData.totalAreaDecimalPlaces" placeholder="请输入总面积位数" />
      </el-form-item>
      <el-form-item label="面积取舍方式" prop="areaRoundingMode">
        <el-select v-model="formData.areaRoundingMode" placeholder="请选择面积取舍方式">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.ERP_DECIMAL_ROUNDING_MODE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="期初" prop="initialAmount">
        <el-input v-model="formData.initialAmount" placeholder="请输入期初" />
      </el-form-item>
      <el-form-item label="开启信用控制" prop="creditLimitFlag">
        <el-radio-group v-model="formData.creditLimitFlag">
          <el-radio
            v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="信用控制方案" prop="creditLimitScheme">
        <el-input v-model="formData.creditLimitScheme" placeholder="请输入信用控制方案" />
      </el-form-item>
      <el-form-item label="锁单信用额" prop="creditLimitThreshold">
        <el-input v-model="formData.creditLimitThreshold" placeholder="请输入锁单信用额" />
      </el-form-item>
      <el-form-item label="开启临时信用额" prop="tempCreditLimitFlag">
        <el-radio-group v-model="formData.tempCreditLimitFlag">
          <el-radio
            v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="数期延长天数" prop="termExtensionDays">
        <el-input v-model="formData.termExtensionDays" placeholder="请输入数期延长天数" />
      </el-form-item>
      <el-form-item label="提醒信用额" prop="creditLimitAlert">
        <el-input v-model="formData.creditLimitAlert" placeholder="请输入提醒信用额" />
      </el-form-item>
      <el-form-item label="临时信用额" prop="tempCreditLimit">
        <el-input v-model="formData.tempCreditLimit" placeholder="请输入临时信用额" />
      </el-form-item>
      <el-form-item label="临时信用开始时间" prop="tempCreditLimitBegin">
        <el-date-picker
          v-model="formData.tempCreditLimitBegin"
          type="date"
          value-format="x"
          placeholder="选择临时信用开始时间"
        />
      </el-form-item>
      <el-form-item label="临时信用结束时间" prop="tempCreditLimitEnd">
        <el-date-picker
          v-model="formData.tempCreditLimitEnd"
          type="date"
          value-format="x"
          placeholder="选择临时信用结束时间"
        />
      </el-form-item>
      <el-form-item label="临时信用编辑人" prop="tempCreditEditor">
        <el-input v-model="formData.tempCreditEditor" placeholder="请输入临时信用编辑人" />
      </el-form-item>
      <el-form-item label="总公司" prop="headquarters">
        <el-input v-model="formData.headquarters" placeholder="请输入总公司" />
      </el-form-item>
      <el-form-item label="属于" prop="belong">
        <el-select v-model="formData.belong" placeholder="请选择属于">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.CUSTOMER_BELONG)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="生产单默认特价" prop="defaultProductionOrderSpecialOffer">
        <el-radio-group v-model="formData.defaultProductionOrderSpecialOffer">
          <el-radio
            v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <!-- 子表的表单 -->
    <el-tabs v-model="subTabsName">
      <el-tab-pane label="ERP基础资料-客户送货地址" name="baseCustomerDeliveryAddress">
        <BaseCustomerDeliveryAddressForm ref="baseCustomerDeliveryAddressFormRef" :customer-id="formData.id" />
      </el-tab-pane>
      <el-tab-pane label="ERP基础资料-客户备注" name="baseCustomerRemark">
        <BaseCustomerRemarkForm ref="baseCustomerRemarkFormRef" :customer-id="formData.id" />
      </el-tab-pane>
    </el-tabs>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { getIntDictOptions, getStrDictOptions, getBoolDictOptions, DICT_TYPE } from '@/utils/dict'
import { BaseCustomerApi, BaseCustomerVO } from '@/api/erp/basecustomer'
import BaseCustomerDeliveryAddressForm from './components/BaseCustomerDeliveryAddressForm.vue'
import BaseCustomerRemarkForm from './components/BaseCustomerRemarkForm.vue'

/** ERP基础资料-客户资料 表单 */
defineOptions({ name: 'BaseCustomerForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  number: undefined,
  name: undefined,
  customerFullName: undefined,
  customerType: undefined,
  insideGroup: undefined,
  declareContacts: undefined,
  deliveryContacts: undefined,
  financialContacts: undefined,
  factoryBusinessPerson: undefined,
  factorySalesPerson: undefined,
  customerPhone: undefined,
  customerFax: undefined,
  leader: undefined,
  kickbackRate: undefined,
  businessRate: undefined,
  customerAddress: undefined,
  remark: undefined,
  unitPriceRoundingMode: undefined,
  unitPriceDecimalPlaces: undefined,
  totalPriceRoundingMode: undefined,
  totalPriceDecimalPlaces: undefined,
  monthSettlementPeriod: undefined,
  monthSettlementDate: undefined,
  monthSettlementDiscount: undefined,
  paymentMode: undefined,
  currency: undefined,
  defaultUnit: undefined,
  decimalProcessingMode: undefined,
  deliveryNoteAccountSet: undefined,
  deliveryArea: undefined,
  deliveryNoteLetter: undefined,
  deliveryNoteDisplayItems: undefined,
  quoteTaxRate: undefined,
  orderSalesControl: undefined,
  orderGiveOthers: undefined,
  defaultSettlementMode: undefined,
  profitRate: undefined,
  valuationMode: undefined,
  valuationFormula: undefined,
  valuationUnit: undefined,
  deliveryDistance: undefined,
  deliveryDistanceUnit: undefined,
  deliveryNoteRows: undefined,
  customerCategory: undefined,
  itemsStocked: undefined,
  labelFormat: undefined,
  excessQuantityAsSpare: undefined,
  commonQuoteNumber: undefined,
  processedCardboardValuationScheme: undefined,
  defaultCustomerForCardboardProduction: undefined,
  deliveryUnitPriceFrom: undefined,
  markupPricingQuoteNumber: undefined,
  cardboardProductionProductType: undefined,
  customerQuoteNumber: undefined,
  taxType: undefined,
  cardboardProductionQuoteMode: undefined,
  cardboardProductionProcessingFeeFormula: undefined,
  cardboardMaterialAreaUnitPriceFormula: undefined,
  cardboardProcessingFeeScheme: undefined,
  cardboardProductionDiscount: undefined,
  cardboardMarkupPricingSetNumber: undefined,
  accountingVoucherCode: undefined,
  importTemplate: undefined,
  groupOrderImportTemplate: undefined,
  saleOrderImportTemplate: undefined,
  exportScheme: undefined,
  groupOrderExportScheme: undefined,
  taxNumber: undefined,
  bankAccountNumber: undefined,
  bankOfDeposit: undefined,
  bankOfDepositAddress: undefined,
  totalAreaDecimalPlaces: undefined,
  areaRoundingMode: undefined,
  initialAmount: undefined,
  creditLimitFlag: undefined,
  creditLimitScheme: undefined,
  creditLimitThreshold: undefined,
  tempCreditLimitFlag: undefined,
  termExtensionDays: undefined,
  creditLimitAlert: undefined,
  tempCreditLimit: undefined,
  tempCreditLimitBegin: undefined,
  tempCreditLimitEnd: undefined,
  tempCreditEditor: undefined,
  headquarters: undefined,
  belong: undefined,
  defaultProductionOrderSpecialOffer: undefined
})
const formRules = reactive({
  number: [{ required: true, message: '客户编号不能为空', trigger: 'blur' }],
  name: [{ required: true, message: '客户名称不能为空', trigger: 'blur' }],
  customerFullName: [{ required: true, message: '客户全称不能为空', trigger: 'blur' }],
  customerType: [{ required: true, message: '客户类型不能为空', trigger: 'change' }],
  unitPriceRoundingMode: [{ required: true, message: '单价处理方式不能为空', trigger: 'change' }],
  unitPriceDecimalPlaces: [{ required: true, message: '单价小数位不能为空', trigger: 'change' }],
  totalPriceRoundingMode: [{ required: true, message: '金额处理方式不能为空', trigger: 'change' }],
  totalPriceDecimalPlaces: [{ required: true, message: '金额小数位不能为空', trigger: 'change' }],
  monthSettlementPeriod: [{ required: true, message: '月结天数不能为空', trigger: 'change' }],
  monthSettlementDate: [{ required: true, message: '每月对帐日不能为空', trigger: 'blur' }],
  paymentMode: [{ required: true, message: '付款方式不能为空', trigger: 'blur' }],
  currency: [{ required: true, message: '币别不能为空', trigger: 'change' }],
  defaultUnit: [{ required: true, message: '默认单位不能为空', trigger: 'change' }],
  decimalProcessingMode: [{ required: true, message: '小数处理方式不能为空', trigger: 'change' }],
  deliveryArea: [{ required: true, message: '送货区域不能为空', trigger: 'change' }],
  deliveryNoteDisplayItems: [{ required: true, message: '送货单显示套件子件不能为空', trigger: 'blur' }],
  orderSalesControl: [{ required: true, message: '订单销售控制不能为空', trigger: 'change' }],
  orderGiveOthers: [{ required: true, message: '订单允许送给别的客户不能为空', trigger: 'blur' }],
  defaultSettlementMode: [{ required: true, message: '默认结算方式不能为空', trigger: 'change' }],
  itemsStocked: [{ required: true, message: '配件生产是否入库不能为空', trigger: 'blur' }],
  excessQuantityAsSpare: [{ required: true, message: '送货单超订单部分转为备品不能为空', trigger: 'blur' }],
  defaultCustomerForCardboardProduction: [{ required: true, message: '自动创建纸板生产单默认客户不能为空', trigger: 'blur' }],
  cardboardProductionQuoteMode: [{ required: true, message: '纸板订单报价方式不能为空', trigger: 'blur' }],
  cardboardProductionDiscount: [{ required: true, message: '纸板生产单折扣不能为空', trigger: 'blur' }],
  totalAreaDecimalPlaces: [{ required: true, message: '总面积位数不能为空', trigger: 'blur' }],
  initialAmount: [{ required: true, message: '期初不能为空', trigger: 'blur' }],
  creditLimitFlag: [{ required: true, message: '开启信用控制不能为空', trigger: 'blur' }],
  creditLimitThreshold: [{ required: true, message: '锁单信用额不能为空', trigger: 'blur' }],
  tempCreditLimitFlag: [{ required: true, message: '开启临时信用额不能为空', trigger: 'blur' }],
  creditLimitAlert: [{ required: true, message: '提醒信用额不能为空', trigger: 'blur' }],
  tempCreditLimit: [{ required: true, message: '临时信用额不能为空', trigger: 'blur' }],
  defaultProductionOrderSpecialOffer: [{ required: true, message: '生产单默认特价不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 子表的表单 */
const subTabsName = ref('baseCustomerDeliveryAddress')
const baseCustomerDeliveryAddressFormRef = ref()
const baseCustomerRemarkFormRef = ref()

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await BaseCustomerApi.getBaseCustomer(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 校验子表单
  try {
    await baseCustomerDeliveryAddressFormRef.value.validate()
  } catch (e) {
    subTabsName.value = 'baseCustomerDeliveryAddress'
    return
  }
  try {
    await baseCustomerRemarkFormRef.value.validate()
  } catch (e) {
    subTabsName.value = 'baseCustomerRemark'
    return
  }
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as BaseCustomerVO
    // 拼接子表的数据
    data.baseCustomerDeliveryAddresss = baseCustomerDeliveryAddressFormRef.value.getData()
    data.baseCustomerRemarks = baseCustomerRemarkFormRef.value.getData()
    if (formType.value === 'create') {
      await BaseCustomerApi.createBaseCustomer(data)
      message.success(t('common.createSuccess'))
    } else {
      await BaseCustomerApi.updateBaseCustomer(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    number: undefined,
    name: undefined,
    customerFullName: undefined,
    customerType: undefined,
    insideGroup: undefined,
    declareContacts: undefined,
    deliveryContacts: undefined,
    financialContacts: undefined,
    factoryBusinessPerson: undefined,
    factorySalesPerson: undefined,
    customerPhone: undefined,
    customerFax: undefined,
    leader: undefined,
    kickbackRate: undefined,
    businessRate: undefined,
    customerAddress: undefined,
    remark: undefined,
    unitPriceRoundingMode: undefined,
    unitPriceDecimalPlaces: undefined,
    totalPriceRoundingMode: undefined,
    totalPriceDecimalPlaces: undefined,
    monthSettlementPeriod: undefined,
    monthSettlementDate: undefined,
    monthSettlementDiscount: undefined,
    paymentMode: undefined,
    currency: undefined,
    defaultUnit: undefined,
    decimalProcessingMode: undefined,
    deliveryNoteAccountSet: undefined,
    deliveryArea: undefined,
    deliveryNoteLetter: undefined,
    deliveryNoteDisplayItems: undefined,
    quoteTaxRate: undefined,
    orderSalesControl: undefined,
    orderGiveOthers: undefined,
    defaultSettlementMode: undefined,
    profitRate: undefined,
    valuationMode: undefined,
    valuationFormula: undefined,
    valuationUnit: undefined,
    deliveryDistance: undefined,
    deliveryDistanceUnit: undefined,
    deliveryNoteRows: undefined,
    customerCategory: undefined,
    itemsStocked: undefined,
    labelFormat: undefined,
    excessQuantityAsSpare: undefined,
    commonQuoteNumber: undefined,
    processedCardboardValuationScheme: undefined,
    defaultCustomerForCardboardProduction: undefined,
    deliveryUnitPriceFrom: undefined,
    markupPricingQuoteNumber: undefined,
    cardboardProductionProductType: undefined,
    customerQuoteNumber: undefined,
    taxType: undefined,
    cardboardProductionQuoteMode: undefined,
    cardboardProductionProcessingFeeFormula: undefined,
    cardboardMaterialAreaUnitPriceFormula: undefined,
    cardboardProcessingFeeScheme: undefined,
    cardboardProductionDiscount: undefined,
    cardboardMarkupPricingSetNumber: undefined,
    accountingVoucherCode: undefined,
    importTemplate: undefined,
    groupOrderImportTemplate: undefined,
    saleOrderImportTemplate: undefined,
    exportScheme: undefined,
    groupOrderExportScheme: undefined,
    taxNumber: undefined,
    bankAccountNumber: undefined,
    bankOfDeposit: undefined,
    bankOfDepositAddress: undefined,
    totalAreaDecimalPlaces: undefined,
    areaRoundingMode: undefined,
    initialAmount: undefined,
    creditLimitFlag: undefined,
    creditLimitScheme: undefined,
    creditLimitThreshold: undefined,
    tempCreditLimitFlag: undefined,
    termExtensionDays: undefined,
    creditLimitAlert: undefined,
    tempCreditLimit: undefined,
    tempCreditLimitBegin: undefined,
    tempCreditLimitEnd: undefined,
    tempCreditEditor: undefined,
    headquarters: undefined,
    belong: undefined,
    defaultProductionOrderSpecialOffer: undefined
  }
  formRef.value?.resetFields()
}
</script>
